<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\admin\model\goods\Order;
use app\common\controller\Api;
use app\common\controller\Payment;
use think\Controller;
use think\Db;
use think\Request;


class Dynamicpraises extends Api
{
    private static $model;
//    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';


    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        self::$model = new \app\common\model\DynamicPraises();
    }

    /**
     * 点赞列表
     */
    public function lists()
    {
        $userId = $this->auth->id;
        $where = [
            'user_id' => $userId
        ];

        $ids_arr = \app\common\model\UserDynamics::where($where)->column('id');
        $pageSize = input('pagesize', 15);

        $lists = self::$model->where('dynamic_id', 'in', $ids_arr)->order('createtime', 'desc')->paginate($pageSize);
        if ($lists) {
            foreach ($lists as $val) {
                $val->user;
            }
        }

        $this->success('点赞列表', $lists);
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function create()
    {
        $dynamicId = input('dynamic_id');
        if (!$dynamicId) {
            $this->error('参数缺失！');
        }

        $where = [
            'user_id' => $this->auth->id,
            'dynamic_id' => $dynamicId,
        ];

        $row_info = self::$model->where($where)->find();
        if (!$row_info) {
            $pdata = [
                'user_id' => $this->auth->id,
                'dynamic_id' => $dynamicId,
            ];

            $res = self::$model->create($pdata);
            if (!$res) {
                $this->error('点赞失败！');
            }

            \app\common\model\UserScoreLog::praiseBonus($this->auth->id);

            $this->success('点赞成功！');
        } else {
            $res = self::$model->where('id', $row_info->id)->delete();
            if (!$res) {
                $this->error('取消点赞失败！');
            }

            \app\common\model\UserScoreLog::praiseBonus($this->auth->id, 0);

            $this->success('取消点赞成功！');
        }
    }
}
